<template>
  <div class="el_main_con">
    <Breadcrumb></Breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入搜索商品" v-model="getGoodsParams.query" clearable @clear="getGoodsList()">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="gotoAdd()">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="180">
        </el-table-column>
        <el-table-column prop="goods_number" label="数量" width="180">
        </el-table-column>
        <el-table-column prop="goods_weight" label="重量" width="180">
        </el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.goods_state === 3">已通过</el-tag>
            <el-tag type="warning" v-else-if="scope.row.goods_state === 2">审核中</el-tag>
            <el-tag type="danger" v-else>未通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dft}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <div class="controllerbox">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteGoods(scope.row)"></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="paginationBox">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="getGoodsParams.pagenum" :page-sizes="[10,20,50,100]" :page-size="getGoodsParams.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { Goods } from '@/api/apiConfig'
import Breadcrumb from 'components/breadcrumb/breadcrumb'
export default {
  components: {
    Breadcrumb
  },
  data () {
    return {
      getGoodsParams: {
        // 查询参数可以为空
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    }
  },
  mounted () {
    this.getGoodsList()
  },
  methods: {
    getGoodsList () {
      const params = this.getGoodsParams
      Goods.getGoodslist(params).then(res => {
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.goodsList = res.data.goods
        this.total = res.data.total
      })
    },
    handleSizeChange (newSize) {
      this.getGoodsParams.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.getGoodsParams.pagenum = newPage
      this.getGoodsList()
    },
    deleteGoods (row) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = `goods/${row.goods_id}`
        Goods.delGoodsById(url).then(res => {
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          this.getGoodsList()
        })
      }).catch(() => { })
    },
    gotoAdd () {
      this.$router.push({
        path: '/goods/add'
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
